package com.spontaneappnew.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.spontaneappnew.Categories;
import com.spontaneappnew.Category;
import com.spontaneappnew.R;

public class CategoriesAdapter extends ArrayAdapter<Category> {

	public ArrayList<Category> CategoryList;
	private LayoutInflater li;
	
	public CategoriesAdapter(Context context, int textViewResourceId,
		   ArrayList<Category> CategoryList) {
		   super(context, textViewResourceId, CategoryList); 
		   this.CategoryList = CategoryList;
		   this.li = LayoutInflater.from(context);
	}
	
	private class ViewHolder {
		  CheckBox name;
		  ImageView icon;
	}
	
	private class SelectedViewHolder {
		TextView name;
		ImageView icon;
	}
	
	private class HeadingHolder{
		TextView label;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null ;
        Category category = this.getItem(position);
        if (category.getType() == Category.TYPE_NORMAL) {
            view = getCategoryView(convertView, parent, category);
            }
        else if (category.getType() == Category.TYPE_SELECT) {
            view = getSelectedCategoryView(convertView, parent, category);
        }
        else {
        	 view = getSelectedHeaderView(convertView, parent, category);
        }
        return view;
    }
		
	private View getSelectedCategoryView(View convertView, ViewGroup parent,
			Category category) {
			SelectedViewHolder holder = null;
			if (convertView == null) {	  
			   convertView = li.inflate(R.layout.selected_category_list_row_layout, null);
		  	   holder = new SelectedViewHolder();
		  	   holder.name = (TextView) convertView.findViewById(R.id.selected_row_textView);
		  	   holder.icon = (ImageView) convertView.findViewById(R.id.selected_row_imageView);
		  	   convertView.setTag(holder);
		  	   holder.icon.setOnClickListener( new View.OnClickListener() { 
		    	   public void onClick(View v) {
		    		   ImageView iv = (ImageView) v;
		  			   Category category = (Category) iv.getTag();
		  			   if (Categories.getInstance().removeFromSelected(category.getID())){
		  				    category.unSelect();
	    			    	notifyDataSetChanged();
		  			   } 
		    	   }	   
		  	   });
		   }
		   else {
				   	holder = (SelectedViewHolder) convertView.getTag();
		   }
		   holder.name.setText(category.getName());
		   holder.icon.setTag(category); 
		   return convertView;  
	}
	


	private View getSelectedHeaderView(View convertView, ViewGroup parent,
			Category category) {
		    HeadingHolder holder = null;
			if (convertView == null) {	  
			   convertView = li.inflate(R.layout.selected_row_heading, null);
			   holder = new HeadingHolder();
			   holder.label = (TextView) convertView.findViewById(R.id.row_heading_text);
			   convertView.setTag(holder);
			   
		    }
			else {
				holder = (HeadingHolder) convertView.getTag();
			}
			holder.label.setText(category.getName());
		    return convertView;  
	}
	
	
	public View getCategoryView( View convertView, ViewGroup parentView, Category category ) {	
		ViewHolder holder = null;
		if (convertView == null) {	  
		   convertView = li.inflate(R.layout.category_list_row_layout, null);
	  	   holder = new ViewHolder();
	  	   holder.name = (CheckBox) convertView.findViewById(R.id.category_checkBox);
	  	   holder.icon = (ImageView) convertView.findViewById(R.id.category_imageview);
	  	   convertView.setTag(holder);
	       holder.name.setOnClickListener( new View.OnClickListener() { 
	    	   public void onClick(View v) { 
	    		   CheckBox cb = (CheckBox) v ; 
	    		   Category category = (Category) cb.getTag();  
	    		   category.setSelected(cb.isChecked());
	    		   if (category.isSelected()){
	    			   Categories.getInstance().getCategories().add(1,new Category(category.getID(), category.getName(),true));
	    			   notifyDataSetChanged();
	    		   } else {
	    			    if (Categories.getInstance().removeFromSelected(category.getID()))
	    			    	notifyDataSetChanged();
	    		   }
	    	   } 
	       }); 
	   }
	   else {
			   	holder = (ViewHolder) convertView.getTag();
	   }
	   holder.name.setText(category.getName());
	   holder.icon.setImageBitmap(BitmapFactory.decodeByteArray(category.getImage(), 0, category.getImage().length));
	   holder.name.setChecked(category.isSelected());
	   holder.name.setTag(category); 
	   return convertView;  
  }
	
	@Override
    public int getViewTypeCount() {
        return 3;
    }
    
    @Override
    public int getItemViewType(int position) {
        return this.getItem(position).getType();
    }
}
